package com.bwie.system.service;

import cn.hutool.core.util.RandomUtil;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.YouHui;
import com.bwie.common.domain.YouXiang;
import com.bwie.common.utils.JwtUtils;
import com.bwie.system.mapper.YouMapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class YouServiceImpl implements YouService {

    @Autowired
    YouMapper youMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public void youAdd(YouHui youHui) {
//        String token = request.getHeader("token");
//        String userKey = JwtUtils.getUserKey(token);
//        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
//        if (user==null){
//            throw  new RuntimeException("该用户没有登录，请及时登录");
//        }
//        if (!youHui.getYouhuiName().matches("^{25}$")){
//            throw  new RuntimeException("优惠券名称最多输入为25个字符");
//        }
        String ss ="YHQ";
        String codes = RandomUtil.randomNumbers(3);
        DateTime dateTime = new DateTime();
        String ccode=ss+dateTime+codes;
        youHui.setYouCodee(ccode);
        if (youHui.getNum()==null){
            throw  new RuntimeException("数量不能为空");
        }
        if (youHui.getManprice()==null){
            throw  new RuntimeException("价格不能为空");
        }
        if (youHui.getJianprice()==null){
            throw  new RuntimeException("减价不能为空");
        }
        youMapper.youAdd(youHui);

    }

    @Override
    public void youdel(Integer youId) {
        youMapper.youdel(youId);
    }

    @Override
    public void updd(YouXiang youXiang) {
        String token = request.getHeader("token");
//        String userKey = JwtUtils.getUserKey(token);
//        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
//        if (user==null){
//            throw  new RuntimeException("该用户没有登录，请及时登录");
//        }
        youMapper.updd(youXiang);
    }

    @Override
    public void upddde(YouXiang youXiang) {
        String token = request.getHeader("token");
//        String userKey = JwtUtils.getUserKey(token);
//        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
//        if (user==null){
//            throw  new RuntimeException("该用户没有登录，请及时登录");
//        }
        youMapper.upddde(youXiang);
    }


}
